//app.js
var api = require('/api.js');

App({
  onLaunch: function () {
    this.judgment()
  },

  getcurrentPage: function () {
    var pages = getCurrentPages()    //获取加载的页面
    var currentPage = pages[pages.length - 1]    //获取当前页面的对象
    console.log(currentPage);
    var url = currentPage.route    //当前页面url
    console.log(url);
    
    return url;
  },

  getcurrentPageStr: function () {
    var pages = getCurrentPages()    //获取加载的页面
    var currentPage = pages[pages.length - 1]    //获取当前页面的对象
    var url = currentPage.route    //当前页面url
    var str = currentPage.options.str;
    url = url + "?str=" + str;
    console.log(url);

    return url;
  }, 

  getcurrentPageId: function () {
    var pages = getCurrentPages()    //获取加载的页面
    var currentPage = pages[pages.length - 1]    //获取当前页面的对象
    var url = currentPage.route    //当前页面url
    var id = currentPage.options.id;
    url = url + "?id=" + id;
    console.log(url);
    
    return url;
  }, 

  getcurrentPageLessonId: function () {
    var pages = getCurrentPages()    //获取加载的页面
    var currentPage = pages[pages.length - 1]    //获取当前页面的对象
    // console.log(currentPage);
    var url = currentPage.route    //当前页面url
    var lessonId = currentPage.options.lessonId;
    url = url + "?lessonId=" + lessonId;
    console.log(url);
    
    return url;
  }, 

  bindparent:function(id){
    console.log("——————————————————————————开始bindparent————————————————————————————");
    console.log('开始绑定'+id);
    var access_token = wx.getStorageSync('access_token');
    if (!access_token){
    }else{
      wx.request({
        url: api.passport.bindParent,
        method: "get",
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          access_token: access_token,
          parent_id: id
        },
        success: function (res) {
          console.log("bindparent的request成功");
          console.log(res);
          wx.setStorageSync("bindparent_id", 0);
          wx.setStorageSync("parent", '有人介绍');
        }
      });
    }
    console.log("——————————————————————————结束bindparent————————————————————————————");
  },

  
  //登录
  login: function () {
    
    var that = this;
    // 获取code
    wx.login({
      success: function (res) {
        console.log(res);
        if (res.code) {
          var code = res.code;
          var user_info = wx.getStorageSync('user_info');
          var encrypted_Data = wx.getStorageSync('encryptedData');
          var iv = wx.getStorageSync('iv');
          var signature = wx.getStorageSync('signature');
          console.log(encrypted_Data)
          //储存code
          wx.setStorageSync("code", code);
          //发送请求
          wx.request({
            url: api.passport.login,
            method: "post",
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            data: {
              code : code ,
              user_info: user_info,
              encrypted_data: encrypted_Data,
              iv: iv,
              signature: signature
            },
            
            success:function(res){
              console.log(res)

              wx.setStorageSync("access_token", res.data.data.access_token);
              wx.setStorageSync("user_id", res.data.data.id);
              wx.setStorageSync("parent", res.data.data.parent);
              
              
              if (res.data.msg == "success"){

                that.judge();

              }else{
                // wx.showToast({
                //   title: res.data.msg,
                //   icon: 'none',
                // })
              }
              
            }
          });

        }
      }
    })
  },

  
  //判断用户是否注册过
  judge: function () {
    var access_token = wx.getStorageSync('access_token');
    wx.request({
      url: api.passport.user,
      method: "get",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        access_token: access_token,
      },
      success: function (res) {
        console.log(res);
        if(res.data.code == 0){
          wx.navigateBack({
            delta: 1
          })
          
        } else if (res.data.code == 1){
          
          wx.navigateTo({
            url: '/pages/login/login',
          })
        }
      }
    });
  },


  //绑定上下线 
  bind:function(){ 
    console.log("——————————————————————————开始bind————————————————————————————");
    var p = getCurrentPages();
    var parent = wx.getStorageSync('parent');
    console.log(p);
    console.log(parent);
    var scene = decodeURIComponent(p[0].options.scene);
    if (p[0].options.user_id){
      wx.setStorageSync("bindparent_id", p[0].options.user_id);
    }
    if (scene != 'undefined'){
      wx.setStorageSync("bindparent_id", scene);
    }
    var bindparent_id = wx.getStorageSync("bindparent_id");
    console.log(bindparent_id);
    if (bindparent_id && parent == '无人介绍') {
      this.bindparent(bindparent_id);
    }
    else if (scene != 'undefined' && parent == '无人介绍'){
      this.bindparent(scene);
    }
    else if (bindparent_id != undefined && parent == '无人介绍'){
      this.bindparent(bindparent_id);
    }
    else if (parent == '无人介绍') {
      this.bindparent(18);
    }
    console.log("——————————————————————————结束bind————————————————————————————");
  },
  
  // 收藏与取消收藏
  collect: function (sectionId) {
    var that = this;
    var access_token = wx.getStorageSync('access_token');
    wx.request({
      url:api.study.changeCollection,
      method: "post",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        access_token: access_token,
        section_id: sectionId,
      },
      success: function (e) {
        console.log(e);
        var msg = e.data.msg;
        if (e.data.code == 0) {
          wx.showToast({
            title: msg,
            icon: 'none',
          });
        } else {
          wx.showToast({
            title: msg,
            icon: 'none',
          });
        }
      }
    });
  },

  //判断设备信息
  judgment:function(){
    var page = this;
    wx.getSystemInfo({
      success: function (res) {
        console.log(res)
        wx.setStorageSync("equipmentSystem", res.platform);
      }  
    })
  }

})